if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Inject_BWH_DailyPatientCareCharge]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[Inject_BWH_DailyPatientCareCharge]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


/***************************************************************************
* Name:			Inject_BWH_DailyPatientCareCharge
* Purpose:		Using table Staging table, Inject patient care charge
*				data depending on the institution and if it is daily or
*				monthly charges.
*
* PARAMETERS
* Name				Description					
* -------------		-----------------------------------------------------------
* n/a
*
* ERRORS USED		Description
* -------------		-----------------------------------------------------
* n/a
*
* RETURN VALUE
* Value    			Description					
* --------------	------------------------------------------------------
* @_exitStatus		An exit status of either true or false
***************************************************************************/
CREATE PROCEDURE [dbo].[Inject_BWH_DailyPatientCareCharge]
AS
BEGIN

	SET NOCOUNT ON

	Begin Try

	Begin Transaction Inject_BWH_DailyPCareCharge

	INSERT INTO [dbo].[PatientCareExpenseDetail]
			   ([AgrFundId]
			   ,[PatientId]
			   ,[PostDate]
			   ,[ServiceDate]
			   ,[FeedDate]
			   ,[Quantity]
			   ,[Amount]
			   ,[Discount]
               ,[ExtendedAmount]
               ,[IsDaily]
               ,PatientCareChargeCodeId
               ,PatientCareEffectivePriceId
				,FundMRN )
		Select
			   FundId
			   ,PatientId
			   ,ValidatedPostDate
			   ,ValidatedServiceDate
			   ,FeedDate
			   ,s.ValidatedQuantity
			   ,s.ValidatedExtendedAmount
               ,s.Discount
			   ,s.NetAmount
               ,1
               ,PatientCareChargeCodeId
               ,PatientCareEffectivePriceId
				, FundMRN 
		from dbo.stg_BWH_DailyPatientCareCharge s
		where s.IsValid = 1
	/*
		Update the special billing number on the fund.
	*/
	Update f  
	set FundMRN = s.FundMRN
	from dbo.stg_BWH_DailyPatientCareCharge s 
	inner join dbo.agrFund f on f.id = s.fundid
	where s.IsValid = 1

	Commit Transaction Inject_BWH_DailyPCareCharge

	End Try

    BEGIN CATCH
    DECLARE 
        @ErrorMessage    NVARCHAR(4000),
        @ErrorNumber     INT,
        @ErrorSeverity   INT,
        @ErrorState      INT,
        @ErrorLine       INT,
        @ErrorProcedure  NVARCHAR(200);

    -- Assign variables to error-handling functions that 
    -- capture information for RAISERROR.
    SELECT 
        @ErrorNumber = ERROR_NUMBER(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE(),
        @ErrorLine = ERROR_LINE(),
        @ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-');

    -- Building the message string that will contain original
    -- error information.
    SELECT @ErrorMessage = 
        N'Error %d, Level %d, State %d, Procedure %s, Line %d, ' + 
            'Message: '+ ERROR_MESSAGE();

	IF XACT_STATE() <> 0 Rollback transaction Inject_BWH_DailyPCareCharge

    -- Raise an error: msg_str parameter of RAISERROR will contain
    -- the original error information.
    RAISERROR 
        (
        @ErrorMessage, 
        @ErrorSeverity, 
        1,               
        @ErrorNumber,    -- parameter: original error number.
        @ErrorSeverity,  -- parameter: original error severity.
        @ErrorState,     -- parameter: original error state.
        @ErrorProcedure, -- parameter: original error procedure name.
        @ErrorLine       -- parameter: original error line number.
        );
		return 99
    END CATCH;
END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO